package com.mc.music_web.controller;

import com.github.pagehelper.PageInfo;
import com.mc.music_web.entity.Admin;
import com.mc.music_web.services.AdminService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;


import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class AdminController {

  @Autowired
    private AdminService adminService;

    @RequestMapping("/login")
    public String login(){
      return "admin/login";
  }
    @RequestMapping("/welcome")
    public String welcome(){
        return "admin/welcome";
    }

    @RequestMapping("admin/index")
    public String index(){
        return "admin/index";
    }

    @RequestMapping("/doLogin")
    @ResponseBody
    public String doAdminLogin(Admin admin, HttpSession session){
        //获取service判断登录的结果

           Admin admin1=adminService.login(admin);

        if (admin1.getAdminName()!=null&&admin1.getAdminPassword()!=null){
            session.setAttribute("ADMIN_SESSION", admin1);
            return "/admin/index";
        }

            return "admin/login";


    }



   /* *//**
     * 返回分页查询的json数据
     * http://localhost:8080/czx_blog_war/admin/admin-list?pageNum=1&pageSize=2
     *
     * @param
     * @param
     * @return
     *//*
    @RequestMapping("/admin/admin-list")
    @ResponseBody
    public PageInfo<Admin> selectAllList4(int pageNum){
        PageInfo<Admin> pageInfo = adminService.selectAllList(pageNum);
        return pageInfo;
    }*/
    @RequestMapping("/admin/go")
    public String goAdmin_list(){

        return "/admin/admin-list";
    }

    @RequestMapping("/admin/list")
    @ResponseBody
    public List adminList(){
        List<Admin> list=adminService.selectAllList();
        return list;
    }

    @RequestMapping("/admin/goUpdate")
    @ResponseBody
    public Admin update(Integer adminId) {

        Admin admin = adminService.selectByPrimaryKey(adminId);

        return admin;
    }


    @RequestMapping("/admin/doUpdate")
    @ResponseBody
    public Admin doUpdate(Admin admin) {

        adminService.updateByPrimaryKey(admin);
        return admin;
    }


    @RequestMapping("/admin/goLimit")
    @ResponseBody
    public PageInfo selectLimit(int pageNum,HttpSession session) {
        PageInfo<Admin> pageInfo = adminService.selectLimit(pageNum);
        session.setAttribute("adminSession",pageInfo);
        return pageInfo;
    }


    @RequestMapping("/admin/del")
    @ResponseBody
    public int del(Admin admin) {
        int result = adminService.delteAdmin(admin);

        return result;
    }

    @RequestMapping("/admin/add")
    public String add(){
        return "/admin/add";
    }

    @RequestMapping("/admin/addAdmin")
    @ResponseBody
    public Integer addAdmin(Admin admin){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        admin.setUpdateTime(sdf.format(new Date()));
        admin.setCreateTime(sdf.format(new Date()));
        admin.setIsDel(0);
        Integer i = adminService.insert(admin);
        return i;
    }

     @RequestMapping("/admin/adminBy")
     @ResponseBody
     public  List  selectByChoose(String keyword){
         List<Admin> list=adminService.selectByChoose(keyword);
         return list;
     }
}
